﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;

namespace VSM
{
    public partial class ForecastsList : System.Web.UI.Page
    {
        private static string FromStartDate = new DateTime(DateTime.Now.Year, 1, 1).ToString("MM/yyyy");
        private static string ToStartDate = new DateTime(DateTime.Now.Year, 12, 1).ToString("MM/yyyy");
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[Utils.AppConstants.ID_User] == null
                || Session[Utils.AppConstants.Password] == null
                || Session[Utils.AppConstants.ID_Dealer] == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                if (Request.QueryString["DocType"] != null && Request.QueryString["DocType"].Length > 0)
                {
                    if (!IsPostBack)
                    {
                        hdnDocType.Value = Request.QueryString["DocType"];
                        txtFromStartDate.Text = FromStartDate;
                        txtToStartDate.Text = ToStartDate;
                    }
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            LoadData();
            Timer1.Enabled = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdBP_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                DataTable dt = Session["FCTable"] as DataTable;
                if (dt != null)
                {
                    dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
                    Session["FCTable"] = dt.DefaultView.ToTable();
                    this.grvFCT1List.DataSource = dt.DefaultView.ToTable();
                    this.grvFCT1List.DataBind();
                }
            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="column"></param>
        /// <returns></returns>
        private string GetSortDirection(string column)
        {
            string sortDirection = "ASC";
            string sortExpression = ViewState["SortExpression"] as string;

            if (sortExpression != null)
            {
                if (sortExpression == column)
                {
                    string lastDirection = ViewState["SortDirection"] as string;
                    if ((lastDirection != null) && (lastDirection == "ASC"))
                    {
                        sortDirection = "DESC";
                    }
                }
            }

            ViewState["SortDirection"] = sortDirection;
            ViewState["SortExpression"] = column;

            return sortDirection;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdBP_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                GridViewRow row = this.grvFCT1List.SelectedRow;
                DataTable dt = (DataTable)Session["FCTable"];
                if (dt != null)
                {
                    int index = row.DataItemIndex;
                    var data = dt.Rows[index];
                    if (data != null)
                    {
                        Session["ChosenCode"] = data["Code"];
                        Session["ChosenName"] = data["Name"];
                        //ScriptManager.RegisterStartupScript(this, this.GetType(), "OK", "Main.okDialogClick('SelectBP');", true);
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdBP_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");
                    e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)"); 

                    e.Row.Attributes["style"] = "cursor:pointer";
                    e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grvFCT1List, "Select$" + e.Row.RowIndex);
                }
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            FromStartDate = txtFromStartDate.Text;
            ToStartDate = txtToStartDate.Text;
            LoadData();
        }

        protected void btnAccept_Click(object sender, EventArgs e)
        {
            try
            {
                GridViewRow selectedRow = this.grvFCT1List.SelectedRow;
                if (selectedRow == null) return;
                ((Label)selectedRow.FindControl("lblDocType")).Text = hdnDocType.Value; 
                Session["ChosenDocList"] = selectedRow;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "OK", "Main.okDialogClick('SelectDocList');", true);
            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// 
        /// </summary>
        private void LoadData()
        {
            try
            {
                GetDefault getDefault = new GetDefault();
                string dbType = Session[Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;

                DateTime fromStartDate = Utils.AppConstants.ConvertToDate((string.IsNullOrEmpty(txtFromStartDate.Text) ? "01/01/1900" : "01/" + txtFromStartDate.Text));
                DateTime toStartDate = Utils.AppConstants.ConvertToDate((string.IsNullOrEmpty(txtToStartDate.Text) ? "01/12/9998" : "31/" + txtToStartDate.Text));
                DateTime fromEndDate = Utils.AppConstants.ConvertToDate((string.IsNullOrEmpty(txtFromEndDate.Text) ? "01/01/1900" : "01/" + txtFromEndDate.Text));
                DateTime toEndDate = Utils.AppConstants.ConvertToDate((string.IsNullOrEmpty(txtToEndDate.Text) ? "31/12/9998" : "31/" + txtToEndDate.Text));

                DataSet dsDocList = getDefault.GetForecastsList(dbType, Session[Utils.AppConstants.ID_Dealer].ToString(), 
                    txtFCCode.Text.Trim(), fromStartDate, toStartDate, fromEndDate, toEndDate);

                if (dsDocList != null && dsDocList.Tables != null & dsDocList.Tables[0].Rows.Count > 0) BindDocList(dsDocList.Tables[0]);
            }
            catch (Exception ex)
            { 
            
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tb"></param>
        private void BindDocList(DataTable tb)
        {
            Session["FCTable"] = tb;
            this.grvFCT1List.DataSource = tb;
            this.grvFCT1List.DataBind();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFiletrByName_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable tblDocList = (DataTable)Session["FCTable"];
                if (tblDocList != null && this.txtFCName.Text.Trim().Length > 0)
                {
                    DataView dv = tblDocList.DefaultView;
                    if (!string.IsNullOrEmpty(this.txtFCName.Text.Trim()))
                        dv.RowFilter = "Name like '%" + this.txtFCName.Text.Trim() + "%'";
                    else
                        dv.RowFilter = "";

                    this.grvFCT1List.DataSource = dv.ToTable();
                    this.grvFCT1List.DataBind();
                }
            }
            catch (Exception ex)
            {

            }
        }
    }
}